<?php

namespace app\beautiful\controller\admin\setting;

use app\beautiful\model\logs\SmsSendLogsModel;

use app\beautiful\model\setting\SettingSmsModel;

use app\BaseController;

use app\common\logic\DataSettingLogic;

use app\beautiful\model\logs\SmsRechargeLogsModel;

use app\common\logic\DataLogic;

class Sms extends BaseController{

    public function show()

    {

        if(null === $sms_setting=SettingSmsModel::where([['shop_id','=',$this->request->shopId]])->find()){

            $sms_setting = new SettingSmsModel();

            $sms_setting->shop_id=$this->request->shopId;

            $sms_setting->save();

        }

        $detail=[

            'sms_num'=>$sms_setting->sms_num,

            'integral_change_notice'=>$sms_setting->integral_change_notice,

            'balance_change_notice'=>$sms_setting->balance_change_notice,

            'new_order_notice'=>$sms_setting->new_order_notice,

            'vip_balance_change_notice'=>$sms_setting->vip_balance_change_notice,

            'order_notice_admin'=>$sms_setting->order_notice_admin,

            'order_deliver_notice_member'=>$sms_setting->order_deliver_notice_member,

            'order_write_off_notice_member'=>$sms_setting->order_write_off_notice_member,

            'boss_mobiles'=>$sms_setting->boss_mobiles,

            'appointment_notice_admin'=>$sms_setting->appointment_notice_admin,

            'appointment_notice_member'=>$sms_setting->appointment_notice_member,

            'sure_appointment'=>$sms_setting->sure_appointment,

            'cancel_appointment'=>$sms_setting->cancel_appointment,

            'appointment_notice_service_person'=>$sms_setting->appointment_notice_service_person,

            'partner_bonus_notice'=>$sms_setting->partner_bonus_notice,

            'consume_notice'=>$sms_setting->consume_notice,
        ];

        return $this->success(['detail'=>$detail]);



    }

    

    public function getSmsPackage(){

        $DataSettingLogic = new DataSettingLogic();

        $sms = $DataSettingLogic->getData('sms');

        if(empty($sms['price'])){

            return $this->error('平台暂未配置短信充值套餐');

        }

        return $this->success([

            'price' => $sms['price'],

        ]);

    }

    

    public function getRechargeLogs(){

        $SmsRechargeLogsModel = new SmsRechargeLogsModel();

        $where = [

             ['shop_id','=',$this->request->shopId],

        ];

        $order=['sms_recharge_id' => 'desc'];

        $field="sms_recharge_id,type,info,sms_num,money,add_time";

        $append=['add_time_format'];

        $return = DataLogic::getDataList($SmsRechargeLogsModel,$where,$order,$field,$this->request->limit,$this->request->page,$append);

        return $this->success($return); 

    }



    public function getSmsSendLogs()

    {

        $SmsSendLogsModel = new SmsSendLogsModel();

        $where = [

            ['shop_id','=',$this->request->shopId],

        ];

        $order=['add_time' => 'desc'];

        $field="sms_send_id,mobile,send_type,after_sms_num,add_time";

        $append=['add_time_format'];

        $return = DataLogic::getDataList($SmsSendLogsModel,$where,$order,$field,$this->request->limit,$this->request->page,$append);

        return $this->success($return);

    }



    public function save()

    {
        $from_type=$this->request->param('from_type/d',1);

        $integral_change_notice=$this->request->param('integral_change_notice/d',0);

        $balance_change_notice=$this->request->param('balance_change_notice/d',0);

        $new_order_notice=$this->request->param('new_order_notice/d',0);

        $vip_balance_change_notice=$this->request->param('vip_balance_change_notice/d',0);

        $boss_mobiles=$this->request->param('boss_mobiles/a',[]);

        $order_notice_admin=$this->request->param('order_notice_admin/d',0);

        $order_deliver_notice_member=$this->request->param('order_deliver_notice_member/d',0);

        $order_write_off_notice_member=$this->request->param('order_write_off_notice_member/d',0);

        $appointment_notice_admin=$this->request->param('appointment_notice_admin/d',0);

        $appointment_notice_member=$this->request->param('appointment_notice_member/d',0);

        $sure_appointment=$this->request->param('sure_appointment/d',0);

        $cancel_appointment=$this->request->param('cancel_appointment/d',0);

        $appointment_notice_service_person=$this->request->param('appointment_notice_service_person/d',0);

        $partner_bonus_notice=$this->request->param('partner_bonus_notice/d',0);

        $consume_notice=$this->request->param('consume_notice/d',0);

        if(!in_array($integral_change_notice,[0,1])) return $this->error('参数错误');

        if(!in_array($balance_change_notice,[0,1])) return $this->error('参数错误');

        if(!in_array($new_order_notice,[0,1])) return $this->error('参数错误');

        if(!in_array($vip_balance_change_notice,[0,1])) return $this->error('参数错误');

        if(!in_array($order_notice_admin,[0,1])) return $this->error('参数错误');

        if(!in_array($order_deliver_notice_member,[0,1])) return $this->error('参数错误');

        if(!in_array($order_write_off_notice_member,[0,1])) return $this->error('参数错误');

        if(!in_array($appointment_notice_admin,[0,1])) return $this->error('参数错误');

        if(!in_array($appointment_notice_member,[0,1])) return $this->error('参数错误');

        if(!in_array($sure_appointment,[0,1])) return $this->error('参数错误');

        if(!in_array($cancel_appointment,[0,1])) return $this->error('参数错误');

        if(!in_array($appointment_notice_service_person,[0,1])) return $this->error('参数错误');

        if($from_type == 2){
            if(!in_array($partner_bonus_notice,[0,1])) return $this->error('参数错误');
            if(!in_array($consume_notice,[0,1])) return $this->error('参数错误');
        }

        

        if($new_order_notice==1 || $vip_balance_change_notice==1){

            if(empty($boss_mobiles)){

                return $this->error('通知手机号不能为空');

            }

            foreach ($boss_mobiles as $k=>$mobile){

                if(!is_mobile($mobile)){

                    return $this->error('第'.($k+1).'个手机号不正确');

                }

            }

        }



        if(null === $sms_setting=SettingSmsModel::where([['shop_id','=',$this->request->shopId]])->find()){

            $sms_setting = new SettingSmsModel();

            $sms_setting->shop_id=$this->request->shopId;

        }



        $sms_setting->integral_change_notice=$integral_change_notice;

        $sms_setting->balance_change_notice=$balance_change_notice;

        $sms_setting->new_order_notice=$new_order_notice;

        $sms_setting->vip_balance_change_notice=$vip_balance_change_notice;

        $sms_setting->order_notice_admin=$order_notice_admin;

        $sms_setting->order_deliver_notice_member=$order_deliver_notice_member;

        $sms_setting->order_write_off_notice_member=$order_write_off_notice_member;

        $sms_setting->appointment_notice_service_person=$appointment_notice_service_person;

        $sms_setting->appointment_notice_admin=$appointment_notice_admin;

        $sms_setting->appointment_notice_member=$appointment_notice_member;

        $sms_setting->sure_appointment=$sure_appointment;

        $sms_setting->cancel_appointment=$cancel_appointment;

        $sms_setting->boss_mobiles=$boss_mobiles;
        
        if($from_type == 2) {
            $sms_setting->partner_bonus_notice = $partner_bonus_notice;
            $sms_setting->consume_notice = $consume_notice;
        }

        $sms_setting->save();



        return $this->success('保存成功');



    }

}